#include <d3d9.h>
#include <d3dx9.h>
#pragma warning( disable : 4996 ) // disable deprecated warning 
#include <strsafe.h>
#pragma warning( default : 4996 )

#pragma comment (lib,"d3d9.lib")
#pragma comment (lib,"d3dx9.lib")
#include "Game.h"
#include "Paddle.h"
#include "Ball.h"



//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------

/*Paddle* g_PL; //Right Paddle
Paddle* g_PR; //Right Paddle
Ball* g_Ball; //Ball*/

//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
	// Create the D3D object, which is needed to create the D3DDevice.
	if( NULL == ( TheGame::Instance()->m_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
		return E_FAIL;

	// Set up the structure used to create the D3DDevice. Most parameters are
	// zeroed out. We set Windowed to TRUE, since we want to do D3D in a
	// window, and then set the SwapEffect to "discard", which is the most
	// efficient method of presenting the back buffer to the display.  And 
	// we request a back buffer format that matches the current desktop display 
	// format.
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory( &d3dpp, sizeof( d3dpp ) );
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

	// Create the Direct3D device.
	if( FAILED( TheGame::Instance()->m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
		D3DCREATE_SOFTWARE_VERTEXPROCESSING,
		&d3dpp, &TheGame::Instance()->m_pd3dDevice ) ) )
	{
		return E_FAIL;
	}

	// Device state would normally be set here

	
	return S_OK;
}

//-----------------------------------------------------------------------------
// Name: wWinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
	
	// Initialize Direct3D
	if( SUCCEEDED( InitD3D( TheGame::Instance()->GetWindowHandle() ) ) )
	{
		// Load Game Elements
		if( SUCCEEDED( TheGame::Instance()->Load() ) )
		{
			// Show the window
			ShowWindow( TheGame::Instance()->GetWindowHandle(), SW_SHOWDEFAULT );
			UpdateWindow( TheGame::Instance()->GetWindowHandle() );

			// Enter the message loop
			MSG msg;
			ZeroMemory( &msg, sizeof( msg ) );
			while( msg.message != WM_QUIT )
			{
				if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
				{
					TranslateMessage( &msg );
					DispatchMessage( &msg );
				}
				else
					TheGame::Instance()->Run();
			}
			TheGame::Instance()->Unload();

		}
	}

	
	return 0;
}



